import { message } from 'antd';
import api from '../../services';
import { history } from 'umi';
const { addCollection, updateCollection, delCollection, queryCollection, queryCollectedArticle } = api;

export default {
    namespace: 'favorite',
    state: {
        collectionList: [],
        articleList: [],
        total: 0,
    },
    subscriptions: {
        setupHistory({ dispatch, history }) {  // eslint-disable-line
            return history.listen(({ pathname, state }) => {
                if (pathname === '/favorite') {
                    dispatch({ type: "queryCollection" });
                    return;
                }
                if (pathname === '/favorite/favoriteDetail') {
                    dispatch({ type: "queryCollectedArticle", payload: { page: 1, pageSize: 10 } });
                    return;
                }
            })
        },
    },
    effects: {
        *queryCollection({ }, { call, put, select }) {
            const { userId } = yield select(state => state.user.userInfo);
            const payload = { userId: userId };
            const response = yield call(queryCollection, payload);
            if (response.code === 0) {
                yield put({
                    type: 'updateState',
                    payload: {
                        collectionList: response.data,
                    },
                });
            } else {
                message.error(response.message, 1);
            }
        },
        *queryCollectedArticle({ payload }, { call, put, select }) {
            const { userId } = yield select(state => state.user.userInfo);
            const collectionId = history.location.state?.collectionId;
            const payloads = { userId, collectionId, ...payload };
            const response = yield call(queryCollectedArticle, payloads);
            if (response.code === 0) {
                yield put({ type: "updateState", payload: { articleList: response.data.records, total: response.data.total } });
            }
            else {
                message.error(response.message, 1);
            }
        },
        *addCollection({ payload }, { call, put }) {
            const response = yield call(addCollection, payload);
            if (response.code === 0) {
                yield put({ type: 'queryCollection' });
                message.success('添加成功');
            } else {
                message.error(response.message, 1);
            }
        },

        *updateCollection({ payload }, { call, put }) {
            const response = yield call(updateCollection, payload);
            if (response.code === 0) {
                message.success('修改成功');
                yield put({ type: 'queryCollection' });
            } else {
                message.error(response.message, 2);
            }
        },

        *delCollection({ payload }, { call, put }) {
            const response = yield call(delCollection, payload);
            if (response.code === 0) {
                message.success('删除成功');
                yield put({ type: 'queryCollection' });
            } else {
                message.error(response.message, 3);
            }
        },
    },

    reducers: {
        updateState(state, action) {
            return { ...state, ...action.payload };
        }
    }
};  